<?php


namespace App\Models;

use Core\Model;
use PDO;

class Doc extends  Model
{
    public $tableName = 'doc';
    public  function __construct() {
        parent::__construct();
    }
    /**
     * Get all the users as an associative array
     *
     * @return array
     */
    public  function getAll()
    {
        $db = static::getDB();
        $stmt = $db->query('SELECT * FROM doc');
        return $stmt->fetchAll();
    }
    public  function Columns(){
        return $this->columns;
    }
    public  function getTypes(){
        $db = static::getDB();

        $stmt = $db->query('select * from doc_type');
        $result = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
        return $result;
    }
    public function docs($id){
        $result = [];
        if(is_integer($id)){
            $db = static::getDB();
            $stmt = $db->query('select id,title from doc where type = '.$id);
            $recv = $stmt->fetchAll();
            foreach ($recv as $key => $value){
                $s['id']= $value['id'];
                $s['title']= $value['title'];
                array_push($result,$s);
            }
            return $result;
        }
    }
    public function topDoc(){
        $result = [];
            $db = static::getDB();
            $stmt = $db->query('select * from doc order by id LIMIT 5');
            $recv = $stmt->fetchAll();
            return $recv;
    }
    public function doc($id){
        if(is_integer($id)){
            $db = static::getDB();
            $stmt = $db->query('select * from doc where type = '.$id);
            $recv = $stmt->fetchAll(FETCH_KEY_PAIR);
            return $recv;
        }
    }
    public function titleDoc($title){
        $db = static::getDB();
        $stmt = $db->query('select * from doc where title = \''.$title.'\'');
        $recv = $stmt->fetchAll();
        return $recv;
    }
}

